package com.hyc.myvideo.ui.view;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;
import android.widget.RemoteViews;
import com.hyc.myvideo.R;
import com.hyc.myvideo.data.api.model.CurriculumHour;
import com.hyc.myvideo.ui.activity.home.AudioActivity;
import com.hyc.myvideo.ui.service.AudioService;

/**
 * 音频播放通知栏
 * Created by dfeads on 2017/8/1.
 */

public class Notifier {
  private static final int NOTIFICATION_ID = 0x222;
  private static AudioService audioService;
  private static NotificationManager manager;

  public static void init(AudioService service) {
    audioService = service;
    manager = (NotificationManager) audioService.getSystemService(Context.NOTIFICATION_SERVICE);
  }

  public static void showPlay(CurriculumHour curriculumHour) {
    audioService.startForeground(NOTIFICATION_ID,
        buildNotification(audioService, curriculumHour, true));
  }

  public static void showPause(CurriculumHour curriculumHour) {
    audioService.stopForeground(false);
    manager.notify(NOTIFICATION_ID, buildNotification(audioService, curriculumHour, false));
  }

  private static Notification buildNotification(Context context, CurriculumHour curriculumHour,
      boolean isPlaying) {
    Intent intent = new Intent(context, AudioActivity.class);
    intent.setAction(Intent.ACTION_VIEW);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    PendingIntent pendingIntent =
        PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    NotificationCompat.Builder builder =
        new NotificationCompat.Builder(context).setContentIntent(pendingIntent)
            .setSmallIcon(R.drawable.ic_notification)
            .setCustomContentView(getRemoteViews(context, curriculumHour, isPlaying));
    return builder.build();
  }

  private static RemoteViews getRemoteViews(Context context, CurriculumHour curriculumHour,
      boolean isPlaying) {
    String title = curriculumHour.getCurriculum_hour_title();
    RemoteViews remoteViews =
        new RemoteViews(context.getPackageName(), R.layout.layout_notification);
    //remoteViews.setImageViewUri(R.id.layout_notification_img, Uri.parse(audio.getImg()));
    remoteViews.setTextViewText(R.id.layout_notification_title,
        curriculumHour.getCurriculum_hour_title());

    return remoteViews;
  }
}
